using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;


namespace SampleAddIn
{
    partial class UI
    {
        void _openMail_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.MailItem mail = (Outlook.MailItem)Globals.ThisApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

            mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
            mail.Attachments.Add(@"c:\ATTACHMENT.txt", Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
            mail.HTMLBody = "<p>This is a sample email.</p><p>Regards.</p>";
            mail.Importance = Outlook.OlImportance.olImportanceHigh;
            mail.FlagStatus = Outlook.OlFlagStatus.olFlagMarked;
            mail.Recipients.Add("sample1@user.com");
            mail.Recipients.Add("sample2@user.com");
            mail.BCC = "sample3@user.com";
            mail.CC = "sample4@user.com";
            mail.Subject = "Sample mail";

            mail.Display(true);
        }

        void _sendMail_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.MailItem mail = (Outlook.MailItem)Globals.ThisApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

            mail.Subject = "Sample send mail";
            mail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
            mail.Body = "Sample mail.\n\nRegards.";
            mail.To = "sample1@user.com; sample2@user.com";

            mail.Send();
        }

        void _markMailAsTask_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.MailItem mail = (Outlook.MailItem)Globals.ThisApplication.CreateItem(Outlook.OlItemType.olMailItem);

            mail.Subject = "Sample mail mark as task";
            mail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
            mail.Body = "Sample mail.\n\nRegards.";
            mail.To = "sample1@user.com; sample2@user.com";

            mail.MarkAsTask(Outlook.OlMarkInterval.olMarkToday);

            mail.Save();
        }

        private void _openAppointment_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)Globals.ThisApplication.CreateItem(Outlook.OlItemType.olAppointmentItem);

            appointment.Subject = "Sample appointment";
            appointment.Body = "This is a sample appointment.";
            appointment.Start = DateTime.Now.AddDays(1);
            appointment.Duration = 60;
            appointment.BusyStatus = Outlook.OlBusyStatus.olBusy;
            appointment.Categories = "Sample appointment category";

            appointment.Display(true);
        }

        private void _createRecurringAppointment_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.AppointmentItem appointment = (Outlook.AppointmentItem)Globals.ThisApplication.CreateItem(Outlook.OlItemType.olAppointmentItem);

            appointment.Subject = "Sample reccurring appointment";
            appointment.Body = "This is a sample reccurring appointment.";
            appointment.Duration = 120;
            appointment.Start = DateTime.Now;

            appointment.GetRecurrencePattern().DayOfWeekMask = Outlook.OlDaysOfWeek.olMonday;
            appointment.GetRecurrencePattern().RecurrenceType = Outlook.OlRecurrenceType.olRecursWeekly;
            appointment.GetRecurrencePattern().Interval = 2;
            appointment.GetRecurrencePattern().NoEndDate = false;
            appointment.GetRecurrencePattern().Occurrences = 3;

            appointment.Save();
        }

        void _createTask_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.TaskItem task = (Outlook.TaskItem)Globals.ThisApplication.CreateItem(Outlook.OlItemType.olTaskItem);

            task.Subject = "Sample task";
            task.Body = "This is a sample task";
            task.Status = Outlook.OlTaskStatus.olTaskNotStarted;
            task.StartDate = DateTime.Now;
            task.Categories = "Sample task category";
            task.DueDate = DateTime.Now.AddDays(2);
            task.Importance = Outlook.OlImportance.olImportanceLow;

            task.Display(true);
        }

        void _assignTask_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.TaskItem task = (Outlook.TaskItem)Globals.ThisApplication.CreateItem(Outlook.OlItemType.olTaskItem);

            task.Subject = "Sample assign task";
            task.Body = "This is a sample assign task";
            task.StartDate = DateTime.Now;
            task.Assign();
            task.Recipients.Add("User 2");

            task.Save();
        }

        void _markCompleteTask_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.TaskItem task = (Outlook.TaskItem)Globals.ThisApplication.CreateItem(Outlook.OlItemType.olTaskItem);

            task.Subject = "Sample complete task";
            task.Body = "This is a sample complete task";
            task.StartDate = DateTime.Now;
            task.MarkComplete();

            task.Save();
        }

        void _filterSearch_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.Folder folder = (Outlook.Folder)Globals.ThisApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
            Outlook.Table table = folder.GetTable("[Start] >= \"" + DateTime.Now.AddMonths(-2).ToShortDateString() + " 0:00 AM" + "\" AND [End] < \"" + DateTime.Now.ToShortDateString() + " 0:00 AM\"", false); 
            Outlook.Row row;

            while((row = table.GetNextRow()) != null)
            {
                System.Windows.Forms.MessageBox.Show(((object[])row.GetValues())[1].ToString());
            }
        }

        void _diplaySearch_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.Folder folder = (Outlook.Folder)Globals.ThisApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

            Outlook.Explorer explorer = Globals.ThisApplication.Explorers.Add(folder, Outlook.OlFolderDisplayMode.olFolderDisplayNormal);
            explorer.Search("subject: tes", true);
            explorer.ShowPane(Outlook.OlPane.olToDoBar, false);
            explorer.Display();
        }

        void _advancedSearch_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.Folder folder = (Outlook.Folder)Globals.ThisApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
            Globals.ThisApplication.AdvancedSearchComplete += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_AdvancedSearchCompleteEventHandler(ThisApplication_AdvancedSearchComplete);
            Globals.ThisApplication.AdvancedSearch(folder.Name, "urn:schemas:calendar:dtend < '2006/05/01 00:00:00' AND urn:schemas:calendar:dtstart > '2006/03/01 00:00:00'", true, Type.Missing);
        }

        void ThisApplication_AdvancedSearchComplete(Microsoft.Office.Interop.Outlook.Search SearchObject)
        {
            Outlook.AppointmentItem result;
            while ((result = (Outlook.AppointmentItem)SearchObject.Results.GetNext()) != null)
            {
                System.Windows.Forms.MessageBox.Show(result.Subject);
            }
        }

        void _exchangeUsers_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.ExchangeUser user = Globals.ThisApplication.Session.CurrentUser.AddressEntry.GetExchangeUser();
            
            System.Windows.Forms.MessageBox.Show(user.FirstName + " " + user.LastName + "," + user.JobTitle);
        }

        void _exchangeUdapteUser_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.ExchangeUser user = Globals.ThisApplication.Session.CurrentUser.AddressEntry.GetExchangeUser();

            user.Comments = "Sample comment";

            user.Update(true, true);
        }

        void _exchangeUdapteDL_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.ExchangeDistributionList distributionList = Globals.ThisApplication.Session.CreateRecipient("sampledl@sample.com").AddressEntry.GetExchangeDistributionList();

            distributionList.Members.Add("user", "Sample user", "user@sample.com"); 
        }

        void _exchangeGetDL_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.ExchangeDistributionList distributionList = Globals.ThisApplication.Session.CreateRecipient("sampledl@sample.com").AddressEntry.GetExchangeDistributionList();

            foreach (Outlook.AddressEntry entry in distributionList.Members)
            {
                System.Windows.Forms.MessageBox.Show(entry.Name);
            }
        }

        void _exchangeGetPF_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.MAPIFolder folder = Globals.ThisApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
            Outlook.ItemsClass items = (Outlook.ItemsClass)folder;
            Outlook.MailItem item = (Outlook.MailItem)items.GetNext();

            while (item != null)
            {
                System.Windows.Forms.MessageBox.Show(item.Subject);
                item = (Outlook.MailItem)items.GetNext();
            }
        }

        void _exchangeUdaptePF_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            Outlook.MAPIFolder folder = Globals.ThisApplication.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
            Outlook.ItemsClass items = (Outlook.ItemsClass)folder;
            Outlook.MailItem mail;

            mail = (Outlook.MailItem)items.Add(Outlook.OlItemType.olMailItem);

            mail.Subject = "Sample mail";
        }
    }
}
